<?php
session_start();
if(isset($_SESSION['userNum'])){
$case = "case1";
$userSes = $_SESSION['userNum'];
}else{
$isLoginCorrect = checkLogin($_POST["uname"],$_POST["password"]);
if($isLoginCorrect == "True"){
$_SESSION['userNum']=$_POST['uname'];  
$case = "case2";
}else{
$case = "case3";
}}?>
<html>
<head>
<title>Login to APC Voting System</title>
<style type="text/css">
.style1 {
	border-collapse: collapse;
}
.style2 {
	border-collapse: collapse;
	background-image: url('images/bg.png');
}
a {
	color: #FFFFFF;
}
</style>
</head>
<body>
<table style="width: 810px; height: 104px" border="0" cellspacing="0" cellpadding="0" class="style1">
<tr>
<td colspan="6" style="height: 104px">
<img src="images\header.png"></td>
</tr>
</table>
<table style="width: 810px" background="images\menu.png" cellspacing="0" cellpadding="0">
<tr >
<td style="width: 11px; height: 40px">
&nbsp;</td>
<td style="width: 84px; height: 40px">
<a href="home.php"><img src="images\homeS.png"></a></td>
<td style="width: 84px; height: 40px">
<a href="vote.php"><img src="images\voteS.png"></a></td>
<td style="width: 84px; height: 40px">
<a href="candidates.php"><img src="images\candidateS.png"></a></td>
<td style="width: 84px; height: 40px">
</td>
<td style="width: 447px; height: 40px">
</td>
<td style="width: 84px; height: 40px">
<a href="logout.php"><img src="images\logoutS.png"></a></td>
</tr>
</table>
<table style="width: 810px; height: 73px" cellspacing="0" cellpadding="0" class="style2">
<tr>
<td style="height: 76px; width: 28px;"></td>
<td style="height: 76px" width="808px">
<?php
if($case == "case1"){
mysql_connect("localhost", "root@localhost", "") or die(mysql_error());
mysql_select_db("apc_vote") or die(mysql_error());

$result = mysql_query("SELECT * FROM voters where voter_num =".$userSes)
or die(mysql_error()); 
	while($row = mysql_fetch_array( $result )){
	echo "<br><h2>Welcome ".$row['voter_fname']." ".$row['voter_lname']."</h2>";
	echo "This is the APC voting system<br>";
	echo "You can view the candidates in the Candidates page<br>";
	echo "or you can vote already in the Vote page<br>";	
	}
}
if($case == "case2"){	
	mysql_connect("localhost", "root@localhost", "") or die(mysql_error());
	mysql_select_db("apc_vote") or die(mysql_error());
	$result = mysql_query("SELECT * FROM voters where voter_num =".$_POST["uname"]." and voter_pass =".$_POST["password"])
	or die(mysql_error()); 
		while($row = mysql_fetch_array( $result )){
		echo "<h2>Welcome ".$row['voter_fname']." ".$row['voter_lname']."</h2>";
		echo "This is the APC voting system<br>";
		echo "You can view the candidates in the Candidates page<br>";
		echo "or you can vote already in the Vote page<br>";
		}
	}		
if($case == "case3"){			
		echo "<br> Please login here <br>";
		echo "<form action='home5.php' method='POST'>";
		echo "<table><tr>";
		echo "<td>Username:</td> <td><input type='text' name='uname' size='30'></td></tr><tr>";
		echo "<td>Password: </td><td><input name = 'password' type = 'Password' Size = '30'></td></tr><tr>";
		echo "<td></td><td><input type='submit' value='Login' align='right'></td>";
		echo "</tr></table></form>";
}

function checkLogin($userNumber,$userPass){
echo "checkLogin";
if($userNumber < 1){
$userNumber = "0000";
$userPass = "0000";
}
mysql_connect("localhost", "root@localhost", "") or die(mysql_error());
mysql_select_db("apc_vote") or die(mysql_error());
$result = mysql_query("SELECT * FROM voters where voter_num= ".$userNumber." AND voter_pass=".$userPass)
or die(mysql_error());
while($row = mysql_fetch_array( $result )){
if($row['voter_num'] < 0){
return "False";
}else{
return "True";
}}}
?>
</td>
</tr>
<tr>
<td width="8px" colspan="2"><img src="images\end.png"></td>
</tr>
</table>
</body>
</html>
